﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;

namespace AesonBaseFramework.Core
{
    public class AesonActionResult
    {
        #region Field
        public bool Succeeded { get; set; }
        public string ReturnMessage { get; set; }
        public Exception Exception { get; set; }
        public object ReturnedObject { get; set; }
        // If a methode succeed but for exemple no change have been committed to database then HasCommunication = true (ex: Authorization already assigned
        public bool HasCommunication { get; set; }
        #endregion


        #region Ctor
        public AesonActionResult()
        {

        }

        public AesonActionResult(bool succeeded, string returnMessage,
                                        Exception exception, object returnedObject, bool hasCommunication)
        {
            Succeeded = succeeded;
            ReturnMessage = returnMessage;
            Exception = exception;
            ReturnedObject = returnedObject;
            HasCommunication = hasCommunication;
        }
        #endregion


        public static AesonActionResult CreateException(Exception exc, bool needToLogInDatabase,
                                                                [CallerMemberName]string callingMethodeName = "",
                                                                  [CallerLineNumber] int sourceLineNumber = 0)
        {
            var r = new AesonActionResult
            {
                Succeeded = false,
                Exception = exc,
                ReturnMessage =
                    "Error in " + callingMethodeName + " at line " + sourceLineNumber + " (" + DateTime.Now.ToString() +
                    ")"
            };
            // create and log the exception
            //TODO send to log
            if (needToLogInDatabase)
            {
                // log to database
            }
            return r;
        }

        public static AesonActionResult CreateSuccededResult(string message, bool needToLogInDatabase,
                                                                    object Object, bool hasCommunication,
                                                                     [CallerMemberName]string callingMethodeName = "",
                                                                          [CallerLineNumber] int sourceLineNumber = 0)
        {
            var r = new AesonActionResult
            {
                Succeeded = true,
                Exception = null,
                ReturnMessage = message,
                HasCommunication = hasCommunication,
                ReturnedObject = Object
            };
            // create and log the exception
            // Warning Object could be null
            //TODO send to log
            if (needToLogInDatabase)
            {
                // log to database           
            }
            return r;
        }


    }
}
